#!/usr/bin/env python
#-*- coding: utf-8 -*-
#__author__:vincentlc
#time: 16/5/31 : 10:29

"""
["..4...63.",".........","5......9.","...56....","4.3.....1","...7.....","...5.....",".........","........."]

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
"""

class Solution(object):
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        for i in range(9):
            row_set = set()
            col_set = set()
            for j in range(9):
                if board[i][j] in row_set:
                    return False
                elif board[i][j]!= '.':
                    row_set.add(board[i][j])

                if board[j][i] in col_set:
                    return False
                elif board[j][i]!= '.':
                    col_set.add(board[j][i])
        return True

    def isValidSudoku2(self, board):
        if len(board)!=9 or len(board[0])!=9:return False
        for i in range(9):
            map=[0 for k in range(10)]
            for j in range(9):
                if board[i][j]=='.':continue
                if board[i][j]<'0' or board[i][j]>'9':return False
                num = ord(board[i][j])-ord('0')
                if map[num]==1:return False
                map[num]=1
        for j in range(9):
            map=[0 for k in range(10)]
            for i in range(9):
                if board[i][j]=='.':continue
                num = ord(board[i][j])-ord('0')
                if map[num]==1:return False
                map[num]=1
        for i in range(0,9,3):
            for j in range(0,9,3):
                map=[0 for k in range(10)]
                for k in range(i,i+3):
                    for l in range(j,j+3):
                        if board[k][l]=='.':continue
                        num = ord(board[k][l])-ord('0')
                        if map[num]==1:return False
                        map[num]=1
        return True

    def isValidSudoku3(self, board):
        if len(board)!=9 or len(board[0])!=9:return False
        for i in range(9):
            map = set()
            for j in range(9):
                if board[i][j]=='.':continue
                if board[i][j]<'0' or board[i][j]>'9':return False
                if board[i][j] in map:
                    return False
                else:
                    map.add(board[i][j])

        for j in range(9):
            map = set()
            for i in range(9):
                if board[i][j]=='.':continue
                if board[i][j] in map:
                    return False
                else:
                    map.add(board[i][j])

        for i in range(0,9,3):
            for j in range(0,9,3):
                map = set()
                for k in range(i,i+3):
                    for l in range(j,j+3):
                        if board[k][l]=='.':continue
                        if board[k][l] in map:
                            return False
                        else:
                            map.add(board[i][j])
        return True

input1 = ["..4...63.",".........","5......9.","...56....","4.3.....1","...7.....","...5.....",".........","........."]
input2 = [".87654321","2........","3........","4........","5........","6........","7........","8........","9........"]
input3 = [["....5..1.",".4.3.....",".....3..1","8......2.","..2.7....",".15......",".....2...",".2.9.....","..4......"],[".87654321","2........","3........","4........","5........","6........","7........","8........","9........"]]

cls_a = Solution()
print(cls_a.isValidSudoku(input1))
print(cls_a.isValidSudoku(input2))

for item in input3:
    print(cls_a.isValidSudoku(item))